# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=ggml
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.0.2100
pkgrel=1
pkgdesc="Tensor library for machine learning. (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/ggml-org/ggml"
license=('spdx:MIT')
depends=(
  "${MINGW_PACKAGE_PREFIX}-cc-libs"
  "${MINGW_PACKAGE_PREFIX}-openblas"
  "${MINGW_PACKAGE_PREFIX}-omp"
  "${MINGW_PACKAGE_PREFIX}-opencl-icd"
  "${MINGW_PACKAGE_PREFIX}-python"
  "${MINGW_PACKAGE_PREFIX}-vulkan-loader"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-opencl-headers"
  "${MINGW_PACKAGE_PREFIX}-shaderc"
  "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
  'git'
)
_commit='6757e3217897ee033a0a3178203b5c4b0254d29e'
source=("${_realname}"::"git+${url}.git#commit=${_commit}")
sha256sums=('0eccd95b81f1d7d02e4cdf5f6ec4559f3ab60e1ba0dd27d090024d574ba08266')

pkgver() {
  cd "${_realname}"

  printf "0.0.%s" "$(git rev-list --count "${_commit}")"
}

prepare() {
  cd ${_realname}
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DGGML_RPC=ON \
      -DGGML_BLAS=ON \
      -DGGML_BLAS_VENDOR=OpenBLAS \
      -DGGML_OPENCL=ON \
      -DGGML_OPENMP=ON \
      -DGGML_VULKAN=ON \
      -DGGML_OPENCL_USE_ADRENO_KERNELS=OFF \
      -DGGML_NATIVE=OFF \
      -DGGML_CCACHE=OFF \
      -DGGML_ALL_WARNINGS=OFF \
      -DGGML_WIN_VER="0x603" \
      -DGGML_BUILD_EXAMPLES=OFF \
      -DGGML_BUILD_TESTS=OFF \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      -S "${_realname}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

check() {
  cmake -DGGML_BUILD_TESTS=ON S"${_realname}" -B"build-${MSYSTEM}"
  cmake --build "build-${MSYSTEM}"

  ctest --test-dir "build-${MSYSTEM}" --output-on-failure
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}"/${_realname}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
